package ns.gflex.domain

import java.util.List;

import ns.gflex.domain.association.RoleMenu;


/**
 * Menu
 * toc 2010-12-14
 * @author wangchu
 */
class Menu implements Comparable<Menu>{
	String app
	String label
	Integer displayOrder = 0
	Long  parentId

	static constraints = {
		app nullable:true
		parentId nullable:true
	}

	@Override
	public int compareTo(Menu o) {
		displayOrder.compareTo(o.displayOrder)
	}

	static init = {

		def roleAdmin = Role.findByRoleCode('Administrators')
		def rolePublic = Role.findByRoleCode('Public')

		def rootMenu = new Menu(label:'Root',app:'').save()

		initAdminMenu(rootMenu.id).each{
			it.save()
			new RoleMenu(role:roleAdmin,menu:it).save()
		}
		initPublicMenu(rootMenu.id).each{
			it.save()
			new RoleMenu(role:rolePublic,menu:it).save()
		}
	}

	private static List initAdminMenu(Long rootId){
		def sys = new Menu(label:'系统设置',app:'',displayOrder:90,parentId:rootId).save()

		[
			new Menu(label:'用户设置',app:'Profile',displayOrder:99,parentId:rootId),
			new Menu(label:'帐号管理',app:'User',displayOrder:2,parentId:sys.id),
			new Menu(label:'角色管理',app:'Role',displayOrder:1,parentId:sys.id),
			new Menu(label:'发布通知',app:'Note',displayOrder:3,parentId:sys.id),
			new Menu(label:'参数维护',app:'Param',displayOrder:4,parentId:sys.id)
		]
	}

	private static List initPublicMenu(Long rootId){
		[
			new Menu(label:'欢迎页面',app:'Welcome',displayOrder:10,parentId:rootId)
		]
	}
}
